#include "seqList.h"
#include <stdlib.h>
#include <stdio.h>
#include <43func.h>

LT *createList()
{
    LT *header = (LT *)malloc(sizeof(LT));
    ERROE_CHECK(NULL, header, "malloc");
    header->count = -1;
    return header;
}
bool insertList(LT *header, type data)
{
    if (isFullList(header))
    {
        printf("Full\n");
        exit(1);
    }

    if (header->count < LEN)
    {
        header->count++;
        header->data[header->count] = data;
        return true;
    }
    else
    {
        return false;
    }
}
void showList(LT *header)
{
    if (isEmptyList(header))
    {
        puts("empty!");
        exit(1);
    }
    for (size_t i = 0; i <= header->count; i++)
    {
        printf("%d ", header->data[i]);
    }
    puts("");
}
// full
bool isFullList(LT *header)
{
    return (LEN - 1 == header->count) ? true : false;
}
// empty
bool isEmptyList(LT *header)
{

    return (-1 == header->count) ? true : false;
}
// length
int lengthList(LT *header)
{
    return header ? header->count + 1 : -1;
}
// select
/// @brief  表头，要查找的值
/// @return 索引，
int selectListVal(LT *header, type val)
{
    if (isEmptyList(header))
    {
        puts("empty");
        exit(1);
    }
    for (size_t i = 0; i <= header->count; i++)
    {
        if (val == header->data[i])
        {
            return i;
        }
    }
    return -1;
}
/// @brief  header   valSrc valDest
/// @return void
void changeListVal(LT *header, type valSrc, type valDest)
{
    int ret = selectListVal(header, valSrc);
    while (ret >= 0)
    {
        header->data[ret] = valDest;
        ret = selectListVal(header, valSrc);
    }
}
// del 按值删除,删第一个
/// @brief  header   val
/// @return void
void delListVal(LT *header, type val)
{
    int index = selectListVal(header, val);
    if (index >= 0)
    {
        for (size_t i = index; i < header->count; i++)
        {
            header->data[i] = header->data[i + 1];
        }
        header->count--;
    }
}
// 清空free
/// @brief  header   
/// @return void
void freeList(LT **header){
    free(*header);
    *header = NULL;
}
void init_header(LT *header){
    header->count = -1;
}